package cn.edu.suda.ada.spider;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Scanner;

import cn.edu.suda.ada.model.SpiderParams;
import cn.edu.suda.ada.parser.AnJuKeUrlParser;
import cn.edu.suda.ada.parser.FiveEightUrlParser;
import cn.edu.suda.ada.parser.GanJiUrlParser;


import cn.edu.suda.ada.queue.StaticUrlQueue;
import cn.edu.suda.ada.queue.StaticVisitedUrlQueue;
import cn.edu.suda.ada.queue.UrlQueue;
import cn.edu.suda.ada.queue.VisitedUrlQueue;
import cn.edu.suda.ada.worker.AnJuKeWorker;
import cn.edu.suda.ada.worker.FiveEigWorker;
import cn.edu.suda.ada.worker.GanJiWorker;


public class SpiderAnJuKe {
	private static UrlQueue urlQueueLevelOne=new UrlQueue();
	
	/**
	 * 准备初始的爬取链接  安居客
	 */
	private static void initializeQueueAnJuKe(String city){
		// 赶集网租房250个一级页面，根据链接规则生成二级URLs放入带抓取队列
//		urlQueueLevelOne.clear();
		for(int i = 1; i <=250; i ++){
			urlQueueLevelOne.addElement("http://"+city+".zu.anjuke.com/fangyuan/p"+i+"-px3/#filtersort");
		}
		System.out.println(urlQueueLevelOne.getUrlQueue());
		
//		if(!urlQueueLevelOne.isEmpty()){
//			System.out.println("获取二级页面URL");
//			AnJuKeUrlParser urlParser=new AnJuKeUrlParser(urlQueueLevelOne);
//			urlParser.getUrlByParse();
//		}
	}
	
	public static void spiderAnJuKe(String city,int theadId){
		urlQueueLevelOne.clear();
		initializeQueueAnJuKe(city);
		
		AnJuKeWorker.setUrlQueueLevelOne(urlQueueLevelOne);
//		System.out.println("开始爬虫(Y/N):");
//		String str2="N";
//		Scanner input2=new Scanner(System.in);
//		str2=input2.next();
		// 创建worker线程并启动Y
//		if("Y".equals(str2)||"y".equals(str2)){
		int k=SpiderParams.WORKER_NUM+theadId-1;
			for(int i = theadId; i <= k; i++){
				new Thread(new AnJuKeWorker(i,city)).start();
			}
//		}
	}
}
